package com.ccx.coachsite.ui.share;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.CustomInfo;
import com.ccx.coachsite.bus.ShareGroupInfo;

public class MyCustomGroupAdapter extends BaseExpandableListAdapter{
	protected Context mContext;
	protected final LayoutInflater mInflater;
	protected List<ShareGroupInfo> mApp;
	protected Map<Integer, groupCheck> selectedMap;
	
	public MyCustomGroupAdapter(Context context, List<ShareGroupInfo> app) {
		mContext = context;
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if(app == null) {
			mApp = Collections.emptyList();
		}
		else
		{
			mApp = app;
		}
		
		selectedMap = new HashMap<Integer, groupCheck>();  
        for (int i = 0; i < app.size(); i++) {
        	groupCheck gc = new groupCheck(app.get(i).getCustoms(), false);
        	gc.isCheck = false;
            selectedMap.put(i, gc);  
        } 
	}
	
	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}
	
	@Override
	public int getChildrenCount(int groupPosition) {
		if(mApp == null || mApp.size() <= groupPosition)
			return 0;
		
		return mApp.get(groupPosition).getCustoms().size();
	}
	
	@Override
	public Object getGroup(int groupPosition) {
		if(mApp == null)
			return 0;
		return mApp.get(groupPosition);
	}
	
	@Override
	public int getGroupCount() {
		if(mApp == null)
			return 0;
		return mApp.size();
	}
	
	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}
	
	@Override
	public boolean hasStableIds() {
		return false;
	}
	
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		if(mApp == null || mApp.size() <= groupPosition)
			return 0;
		
		return mApp.get(groupPosition).getCustoms();
	}
	
	public void resetList(List<ShareGroupInfo> list, boolean isSelected) {
		mApp = null;
		mApp = list;
		selectedMap.clear();
		for (int i = 0; i < list.size(); i++) {
        	groupCheck gc = new groupCheck(list.get(i).getCustoms(), isSelected);
        	gc.isCheck = isSelected;
            selectedMap.put(i, gc);  
        } 
		super.notifyDataSetChanged();
	}
	
	public List<String> getInputs()
	{
		List<String> lstResult = new ArrayList<String>();
		for (int i = 0; i < selectedMap.size(); i++) {  
			groupCheck gc = selectedMap.get(i);
			for(int j = 0; j < gc.childMap.size(); j++)
			{
				if(gc.childMap.get(j))
				{
					boolean isExist = false;
					String mobile = gc.customList.get(j).getTianYi();
					for(int k = 0; k < lstResult.size(); k++)
					{
						if(lstResult.get(k).equals(mobile))
						{
							isExist = true;
							break;
						}
					}
					if(!isExist)
						lstResult.add(mobile);
				}
			}
        }  
		
		return lstResult;
	}
	
	public void doSelectAll(boolean checked)
	{
		for (int i = 0; i < selectedMap.size(); i++) {  
			groupCheck gc = selectedMap.get(i);
			gc.isCheck = checked;
			for(int j = 0; j < gc.childMap.size(); j++)
			{
				gc.childMap.put(j, checked);
			}
        }  
		super.notifyDataSetChanged();
	}
	
	public void doSelectAtIndex(int groupIndex, int index)
	{
		groupCheck gc = selectedMap.get(groupIndex);
		boolean b = !gc.childMap.get(index);
		gc.childMap.put(index, b);  
		super.notifyDataSetChanged();
	}
	
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		ParentHolder parentHolder;
		if(convertView == null) {
			convertView = mInflater.inflate(R.layout.share_group_custom, null);
			parentHolder = new ParentHolder();
			parentHolder.mParentTitle = (TextView) convertView.findViewById(R.id.share_group_title);
			parentHolder.mParentImage = (ImageButton) convertView.findViewById(R.id.share_custom_group_image_item);
			convertView.setTag(parentHolder);
		}else {
			parentHolder = (ParentHolder) convertView.getTag();
		}
		
		parentHolder.mParentTitle.setText(mApp.get(groupPosition).getGroupName());
		parentHolder.mParentImage.setOnClickListener(new OnCheckListener());
		parentHolder.mParentImage.setTag(groupPosition);
		groupCheck gc = selectedMap.get(groupPosition);
		
		if(!gc.isCheck)
			parentHolder.mParentImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ower_image_unchecked));
		else
			parentHolder.mParentImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ower_image_checked));

		return convertView;
	}
	
	class OnCheckListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Integer index = (Integer)v.getTag();
			if(selectedMap.size() > index)
			{
				groupCheck gc = selectedMap.get(index);
				boolean b = !gc.isCheck;
				gc.isCheck = b;
				for(int i = 0; i < gc.childMap.size(); i++)
				{
					gc.childMap.put(i, b);
				}
				
				notifyDataSetChanged();
			}
			
		}
	}
	
	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
			View convertView, ViewGroup parent) {
		List<CustomInfo> mList = mApp.get(groupPosition).getCustoms();
		ViewHolder holder;
		if(convertView == null) {
			convertView = mInflater.inflate(R.layout.share_custom_list_item, parent, false);
			holder = new ViewHolder();
			holder.nUseGprs = (TextView) convertView.findViewById(R.id.share_custom_used_gprs);
			holder.nPhone = (TextView) convertView.findViewById(R.id.share_custom_phone);
			holder.nCheckbox = (ImageView) convertView.findViewById(R.id.share_custom_checkbox_image_item);
			convertView.setTag(holder);
		}else 
		{
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.nUseGprs.setText(mList.get(childPosition).getPackage_gprs());
		holder.nPhone.setText(mList.get(childPosition).getTianYi());
		//holder.nCheckbox.setOnCheckedChangeListener(selectedCheckListener);
		groupCheck gc = selectedMap.get(groupPosition);
		
		if(!gc.childMap.get(childPosition))
			holder.nCheckbox.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ower_image_unchecked));
		else
			holder.nCheckbox.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ower_image_checked));

		return convertView;
	}
	
	class ParentHolder {
		TextView mParentTitle;
		ImageButton mParentImage;
	}
	
	class ViewHolder {
		TextView nUseGprs;
		TextView nPhone;
		ImageView nCheckbox;
	}
	
	class groupCheck{
		public boolean isCheck;
		public Map<Integer, Boolean> childMap;
		public List<CustomInfo> customList;
		public groupCheck(List<CustomInfo> cinfos, boolean isCheck)
		{
			this.customList = cinfos;
			childMap = new HashMap<Integer, Boolean>();  
	        for (int i = 0; i < cinfos.size(); i++) {  
	        	childMap.put(i, isCheck);  
	        } 
		}
	}
}
